package com.atguigu.gmall.order.config;

import com.atguigu.gmall.feign.AppFeignConfig;
import com.atguigu.gmall.mq.AppRabbitConfig;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.stereotype.Component;

import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

@EnableFeignClients(basePackages = {
        "com.atguigu.gmall.feign.ware",
        "com.atguigu.gmall.feign.product",
        "com.atguigu.gmall.feign.cart",
        "com.atguigu.gmall.feign.pay"
})
@Import({AppFeignConfig.class, AppRabbitConfig.class})
@Configuration
public class AppOrderConfig {

    /**
     * 每个项目启动。会创建自己的ioc容器。以后所有的组件会放在这个容器中。
     *    ApplicationContext
     * 组件的名字，只要同一个微服务不重复就没问题
     * JVM --- Feign；feign的配置。是单独维护map的。bean定义重写
     * @param properties
     * @return
     */
    @Bean("corePool")
    public ThreadPoolExecutor executor(ThreadConfigProperties properties) {
        return new ThreadPoolExecutor(properties.core,
                properties.max,
                properties.keepalive, //线程池 1min了都没有活要干了
                TimeUnit.MILLISECONDS,
                new LinkedBlockingDeque<>(properties.queueLength),
                Executors.defaultThreadFactory(),
                new ThreadPoolExecutor.AbortPolicy()
        );
    }

    @Component
    @ConfigurationProperties(prefix = "service.thread")
    @Data
    class ThreadConfigProperties {
        private Integer core = 8;
        private Integer max = 16;
        private Long keepalive = 1000*60L;
        private Integer queueLength = 100000;
    }



}
